In this lecture, we'll explore the menu system in our
Feed the Teddies game. We have five different
menus in this game. This Common folder is just the
Quit menu button textures. But we also have a
Difficulty menu, a Game Over widget that
I'm calling a menu, a HighScore menu, the Main menu, and the Pause menu. We won't bother talking
about the Pause menu, it's implemented just
like I showed you when I showed you how to
implement a Pause menu. Our Main Menu widget is also very similar to
what we've seen before. We have the Play
button that leads us to the Difficulty menu. We have the HighScoreButton that leads us to the HighScore menu, and we have the Quit button
that quits the game. Just to show you that
the Play button is slightly different from what
we've looked at before. If we look at the Event
Graph for the play button, you can see when the
Play button is clicked. Instead of setting
the input mode for the player controller
to Gameplay and moving to the
gameplay level, we set it to be UI Only. That's what it is right
now also but we're going to just do
that to be complete with the things we're
doing as we move along on this button
click and we Set Show Mouse Cursor to
true and then we move to the Difficulty menu and then we remove this widget
from the parent. We'll move along to the
Difficulty menu level from here, setting up the player
controller to make sure we're still interacting
with the user interface. We do similar things with
the HighScoreMenu button, except we go to
the HighScore menu and then the Quit button
is as we've seen before. Let's look at the
HighScore menu next. This one is a little
more complicated. If we look at the event graph, we'll see that not
only do we have processing that we do when
the button is clicked, but I'm actually doing some processing up here
in the construct event. When we actually
build this widget, there are stuff I want to do. The stuff I want to
do is I want to get access to the HighScore
that saved in our SaveGame object and store into a variable right
here in my blueprint. I've declared a SavedHighScore
variable over here. The way you make new
variables is you click "Plus," and then you click here to pick a
data-type including object types and so on
and you give it a name. Now I'm going to
just "Delete" that because I don't actually
want that variable, but that's how I
created that variable. Here when the widget
gets constructed, I load the game from the slot, which we've seen me do in C++ code back in the
save game lecture. But I'm doing it here in
the blueprint instead. I do a typecast to the
SaveGame class that I wrote and then I
access the High Score from that class and set the SavedHighScore variable
over here to that value. Now I've saved the value of the current High Score in
my SavedHighScore variable. We'll see how we actually
use that in a moment. When we click the button, we do the typical stuff. We're going to click the
"Quit" button to go back to the main menu and that's
all as you've seen before. Back in the designer though, you'll see that I have a
vertical box here that has the High Score label and then something that
I call the Value. Obviously when we
go to this menu, it doesn't show Value, it shows the current High Score. How does that actually work? The way that actually works
is over here on the right, what I did was right here, I bound that text to
the SavedHighScore, that variable that I showed you. By binding this textbox
to that variable, I make the textbox display
the value of that variable. By Difficulty menu, as you saw, I have Easy, Medium, and Hard buttons. Let's take a look at the graph. The way this works is I have a variable that I
declared called Difficulty. If the user has clicked
the Easy button, I set that variable to 0. If they've clicked
the Medium button, I set it to 1, and if they've clicked
the Hard button, I set it to 2. At this point, the difficulty
variable holds 0 for easy, 1 for medium, 2 for hard. Then I load my "Save Game." I type cast it. Then, I set the difficulty
property for my saved game object to that
variable that I've set to 0, 1, or 2 for the difficulty, and then I save the
game to the slot. I'm using my Save
Game Slot to store what difficulty the player picked for the game they
were about to play. Then, I set the player
controller to use Game Only, because we're getting
ready to go to Game Play. I don't show the mouse cursor. We would go to Game Play, and I remove from the parent. This is a good way for me to save information
about the difficulty of the game that
we're about to play. When we start playing the game, we'll retrieve that difficulty
information so that we can make the game behave in the appropriate way based
on the selected difficulty. The last menu that we'll look
at is my Game Over menu, which is really a widget that I'll display on top of the game. I didn't show you this
when I demoed the game, but when the game ends, you get this Game Over menu. It tells you your score, and it tells you the
current high score, which may be higher
than your score or the same as your score if you
just got a new score. Then a Quit button
that will let you return to the Main Menu. In our graph, I've declared
two variables here; a saved player score
and a saved high score. When the widget is constructed, I get the Game Mode
because in this game, the Game Mode is the class that keeps track of
the player score. I get the Game Mode
and then I cast it to my Feed The
Teddies Game Mode Base. I access the score property
and I set that variable, Saved Player Score to The Score. Now, saved player score, has gone off to Game Mode. It grabbed the score that
the Game Mode stores, and it stored it in this
variable, Saved Player Score. I load the game from the slot, I cast it to the saved game, and then I get the high score, and I set my Saved High Score
variable to High Score. When I click the "Quit" button, I go to UI only for
the player controller, I show the cursor, I go to the main menu, I remove the widget
from the parent, and I unpause the game. Because when I display
the Game Over widget, I'm actually pausing the
game when I do that, so I want to unpause
the game here. Back in the designer, you'll notice the value of the Player Score is bound
to Saved Player Score, and the value of
the High Score is bound to Saved High Score. Just to show you how that Game Over widget
gets displayed, here in my game mode base, I have an End Game function that gets called when
the game is over. The first thing I do is I try to load from my Saved Game slot, and I've actually included
code in the main menu actor to make sure I have a saved
game in this Saved Game slot. But, we'll be careful anyway. We'll make sure we
actually were able to load a saved game,
and if we were, then we save the high
score property of that saved game instance into my High Score variable that
I've initialized up here. Of course, I can initialize
it like this as well. Online 110. High score is
whatever was saved in the Saved Game instance or zero if for some unexpected reason, I was unable to load
the saved game, and now I check for
a new high score. Again, being careful here, if I do have a new high score, then I set the property of my saved game to
that new High Score, and then I save the
game to the slot. Now that I've done
this processing, the saved game in the slot
holds the current high score, even if the current
high score is one I just scored on this game. Making sure my game over widget class isn't
a null pointer, I have a new property
for this widget that I populate in the
Blueprints editor. I create the widget, make sure it created fine, add it to the viewport, get my player controller, make sure I got my player
controller, and if I did, I set the input
mode to UI only so the player is not playing
the game anymore, they're only able to
interact with this widget, I show them the mouse cursor,
and I pause the game. That's how we
actually get the game over widget displayed
when the game ends. To recap, in this lecture we
explored how to implement a more complicated menu
system in our game including, a High Score menu,
the Difficulty menu, and a Game Over widget
that I also called a Menu.